package com.F01.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;

import com.F01.actionForm.F010101ActionForm;
import com.F01.actionForm.F010106ActionForm;
import com.config.Constants;
import com.dao.common.DbOperationExt;
import com.dbtools.Dispose;
import com.exception.SelectException;
import com.info.T_S_YHCZXXinfo;
import com.util.CommonTools;

/**
 * @wangk071226
 * @客户信息管理
 * @数据库操作类
 * @方法命名规则：静态参数名称、例如：查询方法，方法名 QUERYLIST();
 * @数据库某字段如果的新增值，如果要取当前时间，请在数据库中表时间的字段上设置GETDATE(), @ insert语句执行时，会自动插入时间。避免系统时间于服务器时间不统一造成错误。 @ 例表c_customers的customer_date字段。
 */
public class F010106DAO extends DbOperationExt {

	/**
	 * 记录操作日志;
	 */
	Logger log = Logger.getLogger(F010106DAO.class);

	/**
	 * 构造方法获取数据源;
	 * 
	 */
	public F010106DAO() {
		super();
	}

	public F010106DAO(Connection conn) {
		super(conn);
	}

	/**
	 * @查询
	 */
	public List QUERYLIST(String tablename) {
		StringBuffer msql = new StringBuffer();
		StringBuffer condition = new StringBuffer();
		List list = new ArrayList();
		Map map = null;
		ResultSet rest = null;
		msql.append(" select id as id,code as code,name as name,state as state,remark as remark from ");
		msql.append(tablename);
		msql.append(" where state = '1'  order by id");
		try {
			log.info(" 商务码表 QUERYLIST sql is " + msql.toString());
			rest = super.executeQuery(msql.toString());
			int i=1;
			while (rest.next()) { // 循环结果集并在map中添加结果集每行对应字段的值;
				map = new HashMap();
				map.put("xh", String.valueOf(i));
				map.put("id", CommonTools.null2String(rest
						.getString("id")));
				map.put("code", CommonTools.null2String(rest
						.getString("code")));
				map.put("name", CommonTools.null2String(rest
						.getString("name")));
				map.put("state", CommonTools.null2String(rest
						.getString("state")));
				map.put("remark", CommonTools.null2String(rest
						.getString("remark")));
				list.add(map);
				i++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Dispose.dispose(rest); // 关闭rest及statement;
		}

		return list;
	}

	/**
	 * @新增保存
	 * @要记录操作，在插入后要向操作记录表也插一条语句，也就是用执行多天SQL， @ 所有用super.executeUpdate(sqlList,true)执行
	 */
	public boolean SAVE_INSERT(F010106ActionForm actionForm,String tablename,
			T_S_YHCZXXinfo t_YhczxxInfo) {
		boolean falg = false;
		List sqlList = new ArrayList();
		/** 存放SQL的LIST */
		StringBuffer yhczsql = new StringBuffer();
		/** 操作记录SQL */
		yhczsql = getInsZzxxSQL(t_YhczxxInfo);
		/** 获得操作记录SQL */
		StringBuffer msql = new StringBuffer();
		msql.append(" INSERT INTO "+tablename+"( ");
		msql.append(" code, ");
		msql.append(" name, ");
		msql.append(" remark ");
		msql.append(" )VALUES ( ");
		msql.append(" '" + actionForm.getCode() + "', ");
		msql.append(" '" + actionForm.getName() + "', ");
		msql.append(" '" + actionForm.getRemark()+ "')");

		try {
			log.info("商务码表 SAVE_INSERT sql is " + msql.toString());
			sqlList.add(msql);
			sqlList.add(yhczsql);
			falg = super.executeUpdate(sqlList, true);
			actionForm.setErrorFlag("0");/**成功*/
		} catch (SQLException ex) {
			if ("1".equals(String.valueOf(ex.getErrorCode()))) {
				/** ex.getErrorCode()返回一，表示主键冲突 */
				actionForm.setErrorFlag("1");/** 主键冲突! */
			} else {
				actionForm.setErrorFlag("1");/**保存失败*/
			}
		} catch (Exception e) {
			actionForm.setErrorFlag("4");/**  */
			e.printStackTrace();
			super.rollback();

		}
		super.commit();

		return falg;
	}

	/**
	 * @编辑
	 */
	public void EDIT(F010106ActionForm actionForm, String tablename,HttpServletRequest request) {
		ResultSet rest = null;
		StringBuffer msql = new StringBuffer();
		msql.append(" select id as id,code as code,name as name,state as state,remark as remark from ");
		msql.append(tablename);
		msql.append(" where id='"+ CommonTools.null2String(request.getParameter("id").trim()) + "'");
		try {
			log.info("商务码表 EDIT sql is " + msql.toString());
			rest = super.executeQuery(msql.toString());
			while (rest.next()) { // 循环结果集并在map中添加结果集每行对应字段的值;
				actionForm.setId(CommonTools.null2String(rest.getString("id")));
				actionForm.setCode(CommonTools.null2String(rest.getString("code")));
				actionForm.setName(CommonTools.null2String(rest.getString("name")));
				actionForm.setState(CommonTools.null2String(rest.getString("state")));
				actionForm.setRemark(CommonTools.null2String(rest.getString("remark")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Dispose.dispose(rest); // 关闭rest及statement;
		}
	}

	/**
	 * @编辑保存
	 */
	public boolean SAVE_UPDATE(F010106ActionForm actionForm,String tablename,
			T_S_YHCZXXinfo t_YhczxxInfo) throws SelectException {
		boolean flag = true;
		List sqlList = new ArrayList();
		StringBuffer yhczsql = new StringBuffer();
		yhczsql = getInsZzxxSQL(t_YhczxxInfo);
		StringBuffer msql = new StringBuffer();
		msql.append(" UPDATE "+tablename+"  SET ");
		msql.append("   code = '" + actionForm.getCode()
				+ "', ");
		msql.append("   name = '" + actionForm.getName() + "', ");
		msql.append("   remark = '"+ actionForm.getRemark() + "' ");
		msql.append(" WHERE  id = '"+ actionForm.getId() + "' ");
		try {
			log.info("商务码表 SAVE_UPDATE sql is " + msql.toString());
			sqlList.add(msql);
			sqlList.add(yhczsql);
			flag = super.executeUpdate(sqlList, true);
			actionForm.setErrorFlag("0");

		} catch (Exception e) {
			actionForm.setErrorFlag("1");
			e.printStackTrace();
			super.rollback();

		}
		super.commit();
		return flag;
	}

	/**
	 * @删除
	 */
	public boolean DELETE(F010106ActionForm actionForm,String tablename,
			T_S_YHCZXXinfo t_YhczxxInfo, HttpServletRequest request)
			throws SelectException {
		boolean flag = true;
		List sqlList = new ArrayList();
		StringBuffer yhczsql = new StringBuffer();
		yhczsql = getInsZzxxSQL(t_YhczxxInfo);
		StringBuffer msql = new StringBuffer();
		msql.append(" update  "+tablename+" set ");
		msql.append(" state = '0' ");
		msql.append(" WHERE ");
		msql.append(" id = '"+ CommonTools.null2String(actionForm.getId().trim()) + "' ");
		try {
			log.info("sql is " + msql.toString());
			sqlList.add(msql);
			sqlList.add(yhczsql);
			flag = super.executeUpdate(sqlList, true);
			actionForm.setErrorFlag("0");
		} catch (Exception e) {
			e.printStackTrace();
			super.rollback();
			actionForm.setErrorFlag("1");
		}
		super.commit();
		return flag;
	}
	/**
	 * @返回表名
	 * */
	public String tablename(String tablecode) throws SelectException{
		String value = "";
		String sql = "select name as name from  t_s_tablename where code ='"+tablecode+"'";

		ResultSet rest = null;
		try {
			rest = this.executeQuery(sql);
			while (rest.next()) {
				value = CommonTools.null2String(rest.getString("name"));
			}
		} catch (SQLException e) {
			throw new SelectException(e.getMessage());
		} finally {
			Dispose.dispose(rest);
		}
		return value;
	}
	/**
	 * 判断是否重复
	 * */
	public String check(String sql){
		boolean bool = false;
		ResultSet rest = null;
		String bs ="0";
		try {
			log.info("工作好作为主键判断 sql is " + sql);
			rest = super.executeQuery(sql);
			bool = rest.next();
			if(bool){
				bs = "1";
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Dispose.dispose(rest); // 关闭rest及statement;
		}
		return bs;
	}

}
